//
//  PlayTableViewController.m
//  Travel
//
//  Created by lanou3g on 15/12/7.
//  Copyright © 2015年 涂晓雨. All rights reserved.
//

#import "PlayTableViewController.h"
#import "PlayTableViewCell.h"
#import "PlayDataHelper.h"
#import "PlayModel.h"
#import "CityModel.h"
#import "CityTableViewController.h"
#import "UIImageView+WebCache.h"
#import "CityTableViewController.h"
#import "MapViewController.h"
#import "advObject.h"
#import "SDCycleScrollView.h"
#import "DetaiPlayViewController.h"
#import "AppDelegate.h"
@interface PlayTableViewController ()<cityName,SDCycleScrollViewDelegate>
@property(nonatomic,assign)NSInteger index;
@property(nonatomic,strong)UIButton *top_button;
@property(nonatomic,assign)NSInteger buttonY;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)NSMutableArray *PicArray;
@property(nonatomic,strong)NSMutableArray *array;
@end

@implementation PlayTableViewController

//懒加载
-(NSMutableArray *)titleArray{

    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

-(NSMutableArray *)PicArray{
    if (_PicArray == nil) {
        _PicArray = [NSMutableArray array];
    }
    return _PicArray;
}


-(NSMutableArray *)array{
    if (_array == nil) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //设置tableView的边距
//    self.tableView.contentInset = UIEdgeInsetsMake(150,0 ,0,0);
    //添加View
        UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, -44, self.view.frame.size.width, 44)];
        [self.navigationItem setTitleView:topView];
    
    //添加button
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 20, 30)];
    [button setBackgroundImage:[UIImage imageNamed:@"play3.png"] forState:UIControlStateNormal];
    [topView addSubview:button];
    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    
    //添加label
#pragma mark - 把block的值传给label
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, 200, 30)];
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    
        __weak typeof(self) weakSelf = self;
//    app.myblock = ^(NSString *str){
//        weakSelf.label.text = str;
//    };
//    NSLog(@"%@",self.label.text);
    self.label.text = @"北京";
    [topView addSubview:self.label];
    self.cityID = @"12";
    //添加一个button可以进行视图与地图之间的切换
    self.top_button = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 70, self.view.frame.size.height - 100, 50, 50)];
    self.top_button.layer.masksToBounds = YES;
    self.top_button.layer.cornerRadius = 25;
    [self.tableView addSubview:self.top_button];
    self.top_button.backgroundColor = [UIColor redColor];
    [self.tableView bringSubviewToFront:self.top_button];
    self.buttonY=(NSInteger)self.top_button.frame.origin.y;
    [self.top_button addTarget:self action:@selector(MapAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"PlayTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
     self.index = 1;
    [self loadData];
    [self loadPicData];
//    __weak typeof(self.tableView) weakTable = self.tableView;

    [PlayDataHelper sharePlayDataHelper].setValue = ^(NSMutableArray *arr){
        [weakSelf addPic:arr];
    };
   
}

-(void)loadPicData{

   [[PlayDataHelper sharePlayDataHelper]advData:self.tableView];
}
//轮播图
-(void)addPic:(NSArray *)arr{

    SDCycleScrollView *cycle = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,0,self.view.frame.size.width, 170) imageURLStringsGroup:nil];
    cycle.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycle.delegate = self;
    NSMutableArray *pics = [NSMutableArray array];
    NSMutableArray *titles = [NSMutableArray array];
    for (advObject *adv in arr) {
        [pics addObject:adv.NewPic];
        [titles addObject:adv.Title];
    }
    cycle.imageURLStringsGroup = pics;
    cycle.titlesGroup = titles;
    cycle.autoScrollTimeInterval = 5;
    self.tableView.tableHeaderView = cycle;
}



-(void)MapAction{
    MapViewController *map = [[MapViewController alloc]init];
    map.city = self.label.text;
    [self presentViewController:map animated:YES completion:nil];
   
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.top_button.frame = CGRectMake(self.top_button.frame.origin.x, self.buttonY+self.tableView.contentOffset.y , self.top_button.frame.size.width, self.top_button.frame.size.height);
}


-(void)action:(UIBarButtonItem *)sender{
   
    
    CityTableViewController *city = [[CityTableViewController alloc]init];
    city.delegate = self;
    [self.navigationController pushViewController:city animated:YES];

}

//实现协议(逆向传值)
-(void)cityName:(CityModel *)value{

    [self.tableView reloadData];
    self.label.text = value.Name;
    self.cityID = value.Id;
    [self loadData];
}

//加载数据
-(void)loadData{
    [[PlayDataHelper sharePlayDataHelper]tourListData:self.tableView ID:self.cityID pageIndex:1];
    [[PlayDataHelper sharePlayDataHelper]refreshData:self.tableView];
    [[PlayDataHelper sharePlayDataHelper]loadDatatableView:self.tableView ID:self.cityID];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [PlayDataHelper sharePlayDataHelper].TourArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    PlayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[PlayTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    PlayModel *play = [PlayDataHelper sharePlayDataHelper].TourArray[indexPath.section];
    
    [cell.PicImage sd_setImageWithURL:[NSURL URLWithString:play.NewPic]];
    cell.NameLabel.text = play.Name;
    cell.detailLabel.text = play.Foreword;
    cell.detailLabel.alpha = 0.5;
    cell.detailLabel.font = [UIFont systemFontOfSize:12];
    //数据类型转换
    cell.distanceLabel.font = [UIFont systemFontOfSize:13];
    CGFloat dis = play.Distance.floatValue * 100 ;
    if (dis < 200 && dis > 0) {
      NSString *str = [NSString stringWithFormat:@"%.2lfkm",dis];
        
      cell.distanceLabel.text = str;
    }else{
    
        NSString *str = [NSString stringWithFormat:@">200km"];
        
        cell.distanceLabel.text = str;
    
    }
    
 
    
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 120;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetaiPlayViewController *detail = [[DetaiPlayViewController alloc]init];
    PlayModel *play = [PlayDataHelper sharePlayDataHelper].TourArray[indexPath.section];;
    detail.model = play;
    detail.cityName = self.label.text;
    NSLog(@"cityName:%@",detail.cityName);
    [self.navigationController pushViewController:detail animated:YES];
   

}

-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{

    DetaiPlayViewController *detail = [[DetaiPlayViewController alloc]init];
    PlayModel *play = [[PlayModel alloc]init];
    detail.model = play;
    
    [self.navigationController pushViewController:detail animated:YES];

}


@end
